Ext.define('org.jks.AddRoleWindow', {
			extend : 'Ext.window.Window',
			title : '添加角色',
			width : 500,
			height : 360,
			modal : false,
			loadMask: undefined,
			
			initComponent : function() {
				var me = this;
				me.loadMask = new Ext.LoadMask(Ext.getBody(), {msg:"请稍后，正在处理..."});
				me.roleName = Ext.create('Ext.form.field.Text', {
							fieldLabel : '名称',
							id : 'role_name'
						});
				me.roleDesp = Ext.create('Ext.form.field.Text', {
							fieldLabel : '描述',
							id : 'role_desp'
						});
				var privilegeStore = Ext.create('Ext.data.Store', {
							id : 'privilege_store',
							fields : ['Id', 'Name'],
							autoLoad : true,
							proxy : {
								type : 'ajax',
								url : '/privilege/list',
								reader : {
									type : 'json',
									root : 'data.rows'
								}
							}
						});
				me.privilegeGrid = Ext.create('Ext.grid.Panel', {
							id : 'privile_Grid',
							selType : 'checkboxmodel',
							height : 200,
							store : privilegeStore,
							columns : [{
										text : '权限名称',
										flex : 1,
										dataIndex : 'Name'
									}]
						});

				me.okButton = Ext.create('Ext.button.Button', {
							text : '确定',
							handler : function() {
								me.addRole();
							}
						});
				me.cancelButton = Ext.create('Ext.button.Button', {
							text : '取消',
							handler : function() {
								me.close();
							}
						});

				me.items = [{
							xtype : 'container',
							margin : '20 20 0 20',
							items : [me.roleName, me.roleDesp, me.privilegeGrid]
						}];
				me.buttons = [me.okButton, me.cancelButton];

				me.callParent();
			},

			addRole : function() {
				var me = this;
				me.loadMask.show();
				me.hide();
				var name = Ext.getCmp("role_name").getValue();
				var desp = Ext.getCmp("role_desp").getValue();
				var selectionModel = me.privilegeGrid.getSelectionModel();
				var models = selectionModel.getSelection();
				if (Ext.isEmpty(name)) {
					me.loadMask.hide();
					me.show();
					Ext.Msg.alert('错误', '名称不能为空');
					
					return;
				}
				if (Ext.isEmpty(models.length) || models.length <= 0) {
					me.loadMask.hide();
					me.show();
					Ext.Msg.alert('错误', '未指派任何权限');
					return;
				}

				var ids = [];
				for (var i = 0; i < models.length; i++) {
					ids[i] = models[i].get("Id");
				}

				Ext.Ajax.request({
							url : '/role/add',
							params : {
								Name : name,
								Desp : desp,
								privileges : ids
							},
							callback : function(options, success, response) {
								me.loadMask.hide();
								if (success) {
									var result = Ext
											.decode(response.responseText);
									if (result.success) {
										Ext.getCmp("role_grid").getStore()
												.reload();
										me.close();
									}
									else{
										me.show();
										Ext.Msg.alert('错误', result.msg);
									}
								}
								else{
									Ext.Msg.alert('错误', '网络错误');
									me.show();
								}
							}
						});

			},
			init : function(record) {
				var name = Ext.getCmp("role_name");
				var desp = Ext.getCmp("role_desp");
				name.setValue(record.get('Name'));
				desp.setValue(record.get('Desp'));
				this.roleId = record.get('Id');
				this.detailStore = record.get('privileges');
			}
		});